package com.xinli.xinlifm.view.adapter.find;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xinli.xinlifm.R;
import com.xinli.xinlifm.http.bean.player.RecommendBean;

import java.util.Collection;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by zz on 16-10-17.
 */

public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.ViewHolder>{
    private final LayoutInflater inflater;
    private Context context;
    private List<RecommendBean.DataBean> list;

    public RecommendAdapter(Context context, List<RecommendBean.DataBean> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.recommend_recy_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        RecommendBean.DataBean dataBean = list.get(position);
        RecommendBean.DataBean.UserBean user = dataBean.getUser();
        Picasso.with(context).load(user.getAvatar()).placeholder(R.mipmap.touxiang).into(holder.userIcon);
        holder.content.setText(dataBean.getContent());
        holder.userName.setText(user.getNickname());
        holder.time.setText(dataBean.getCreated());
        holder.action.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = inflater.inflate(R.layout.popup_reco_item,null);
                PopupWindow popupWindow = new PopupWindow(view,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setFocusable(true);
                popupWindow.setOutsideTouchable(true);
                int[] location = new int[2];
                holder.action.getLocationOnScreen(location);
                popupWindow.showAtLocation(holder.like,Gravity.NO_GRAVITY,location[0]+popupWindow.getWidth(),location[1]);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public void addAll(Collection collection) {
        list.addAll(collection);
        notifyDataSetChanged();
    }
    static class ViewHolder extends RecyclerView.ViewHolder{

        private final CircleImageView userIcon;
        private final TextView userName;
        private final TextView content;
        private final TextView time;
        private final ImageView action;
        private final ImageView like;

        public ViewHolder(View itemView) {
            super(itemView);
            userIcon = ((CircleImageView) itemView.findViewById(R.id.reco_user_icon));
            userName = ((TextView) itemView.findViewById(R.id.reco_user_name));
            content = ((TextView) itemView.findViewById(R.id.reco_recommend));
            time = ((TextView) itemView.findViewById(R.id.reco_time));
            action = ((ImageView) itemView.findViewById(R.id.reco_action));
            like = ((ImageView) itemView.findViewById(R.id.reco_like));
        }
    }
}
